﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop
{
    public partial class DialogBase : DevComponents.DotNetBar.Office2007Form
    {
        public DialogBase()
        {
            InitializeComponent();
        }

        private bool m_ShowProgressPanel = true;
        [DefaultValue(true)]
        public bool ShowProgressPanel
        {
            get
            {
                return m_ShowProgressPanel;
            }
            set
            {
                m_ShowProgressPanel = value;

                panelExProgress.Visible = m_ShowProgressPanel;
                if (m_ShowProgressPanel)
                    panelExLeft.Height = 380;
                else
                    panelExLeft.Height = 518;
            }
        }

        protected void StartProgressIndicator(string text)
        {
            if (!panelExProgress.Visible)
                return;

            pictureBoxWait.Visible = true;
            labelXWaitText.Text = text;
            labelXWaitText.Visible = true;
        }

        protected void StopProgressIndicator()
        {
            if (!panelExProgress.Visible)
                return;

            pictureBoxWait.Visible = false;
            labelXWaitText.Visible = false;
        }

        protected void StartAsynchWork()
        {
            StartAsynchWork("Processing");
        }

        protected void StartAsynchWork(string progressIndicatorText)
        {
            StartProgressIndicator(progressIndicatorText);
            buttonXOk.Enabled = false;

            if (!backgroundWorkerProcessing.IsBusy)
                backgroundWorkerProcessing.RunWorkerAsync();
        }

        public virtual void DoAsynchWork(DoWorkEventArgs e)
        {
            //do nothing - all work should be implemented in parent
        }

        protected void CancelAsynchWork()
        {
            if (backgroundWorkerProcessing.IsBusy)
                backgroundWorkerProcessing.CancelAsync();
        }

        public virtual void AsychWorkComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            //should be implemented in parent with call to base
            StopProgressIndicator();
            buttonXOk.Enabled = true;
        }

        protected bool AsynchBusy 
        {
            get { return backgroundWorkerProcessing.IsBusy; } 
        }

        protected bool AsynchCancellationPending
        {
            get { return backgroundWorkerProcessing.CancellationPending; }
        }

        private void backgroundWorkerProcessing_DoWork(object sender, DoWorkEventArgs e)
        {
            this.DoAsynchWork(e);

            if ((sender as BackgroundWorker).CancellationPending)
                e.Cancel = true;
        }

        private void backgroundWorkerProcessing_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                return;

            AsychWorkComplete(sender, e);
        }

        private void itemPanelLeft_ItemClick(object sender, EventArgs e)
        {
            //alow only one selected
            foreach (object _Control in itemPanelLeft.Items)
            {
                if (_Control != sender && (_Control as DevComponents.DotNetBar.ButtonItem) != null)
                    (_Control as DevComponents.DotNetBar.ButtonItem).Checked = false;
            }
        }
    }
}
